// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/platform/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_pickup = require("./accountDetainedApplication.css");
	var databind = require("databind");
	var deliveryId;
	var financeId;
	var inventoryId = [];
	var remark;
	var tools = require("tools");

	var init = function(page) {
		deliveryId = page.params.deliveryId;
		financeId = page.params.financeId;
		inquireDeliveryDetail();
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}

		$("#detainedApplicationSubmitBtn").unbind("click").click(function() {
			inventoryId = [];
			remark = "";

			var inventorys = $("#accountDetainedApplication .detainedApplicationChecked:checked");
			$.each(inventorys, function() {
				inventoryId.push($(this).parents("tr").attr("inventoryId"));
			});

			remark = $.trim($("#detainedApplicationReason").val());

			if (inventoryId.length == 0) {
				$("#accountDetainedApplication .error_info").html("请选择要扣货的货箱！");
				$("#accountDetainedApplication .error_info").removeClass("none");
			} else if (remark == "") {
				$("#accountDetainedApplication .error_info").html("请填写扣货缘由！");
				$("#accountDetainedApplication .error_info").removeClass("none");
			} else {
				$("#accountDetainedApplication .error_info").addClass("none");
				createDetainedApplication();
			}
		});
	};

	//获取放货配送明细
	var inquireDeliveryDetail = function() {
		base.fnAjax({
			action: "inquireDeliveryDetail.json",
			async: false,
			oData: {
				deliveryId: deliveryId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#detainedTpl").html();
					var html = juicer(tpl, data);
					$("#detaniedBody").html(html);

					$("#detainedDataListBody").on("click", ".detainedDetail", function() {
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId=" + $(this).parents("tr").attr("inventoryId"));
					});

					$("#detainedDataListBody").on("click", ".detainedLogisticsInfo", function() {
						router.route("logisticsInfo/inquireLogisticsInfoDetail.html?inventoryId=" + $(this).parents("tr").attr("inventoryId"));
					});

					tools.inquireCommonSelectorList(9, $("#accountDetainedApplication .applicantReson"));
				} else {}
			}
		});
	};

	//扣货申请
	var createDetainedApplication = function() {
		base.fnAjax({
			action: "createDetainedApplication.json",
			async: false,
			oData: {
				financeId: financeId,
				remark: remark,
				inventoryId: inventoryId,
				applicantReson: $("#accountDetainedApplication .applicantReson").val()
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					router.route("main/supervisionRelieveList.html");
				} else {
					$("#accountDetainedApplication .error_info").html(data.message);
					$("#accountDetainedApplication .error_info").removeClass("none");
				}
			}
		});
	};

	return {
		init: init
	};
});